package intjavatorpedo.client;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;

public class TorpedoButton extends JButton implements MouseListener {

	private MainWindow mainWindow;
	private int row;
	private int col;
	private boolean opponent;
	
	private boolean shipPart;
	private boolean bombed;
	private boolean bombSuccess;
	
	private Dimension size = new Dimension(30, 30);
	
	public TorpedoButton(MainWindow mainWindow, int row, int col, boolean opponent) {
		this.mainWindow = mainWindow;
		this.row = row;
		this.col = col;
		this.opponent = opponent;
		
		this.setSize(size);
		this.addMouseListener(this);
	}
	
	public void refreshStatusMyCoord(boolean shipPart, boolean bombed) {
		this.opponent = false;
		this.shipPart = shipPart;
		this.bombed = bombed;
	}
	
	public void refreshStatusOpponentCoord(boolean bombed, boolean bombSuccess) {
		this.opponent = true;
		this.bombed = bombed;
		this.bombSuccess = bombSuccess;
	}
	
	@Override
	public Dimension getPreferredSize() {
		return this.size;
	}
	
	@Override
	public Dimension getMinimumSize() {
		return this.getPreferredSize();
	}
	
	@Override
	public Dimension getMaximumSize() {
		return this.getPreferredSize();
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Image image;
		
		if (this.opponent) {
			if (this.bombed) {
				if (this.bombSuccess)
					image = ImageRepo.opponentSuccessful;
				else
					image = ImageRepo.opponentUnsuccessful;
			} else
				image = ImageRepo.normalCell;
		} else {
			if (this.shipPart) {
				if (this.bombed)
					image = ImageRepo.myShipBombed;
				else
					image = ImageRepo.myShipNotBombed;
			} else {
				if (this.bombed)
					image = ImageRepo.myNotShipBombed;
				else
					image = ImageRepo.normalCell;
			}
		}
		
		g.drawImage(image, 0, 0, this.size.width, this.size.height, null);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		this.mainWindow.buttonInput(row, col, opponent);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
